﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UserControlAsset.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the UserControlAsset class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using System.IO;
    using System.Web;

    /// <summary>
    /// Defines the UserControlAsset class.
    /// </summary>
    public class UserControlAsset
    {
        #region Fields
        /// <summary>
        /// The identity of the database table's row where this module object is saved.
        /// </summary>
        private int moduleID;

        /// <summary>
        /// Stores the module type for this module (main column or right column).
        /// </summary>
        private UserControlArea moduleType;

        /// <summary>
        /// Stores the module's given name.
        /// </summary>
        private string moduleName;

        /// <summary>
        /// Stores the module's path.
        /// </summary>
        private string modulePath;

        /// <summary>
        /// Stores the module's description.
        /// </summary>
        private string description;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the UserControlAsset class.
        /// </summary>
        public UserControlAsset()
        {
        }

        /// <summary>
        /// Initializes a new instance of the UserControlAsset class.
        /// </summary>
        /// <param name="moduleId">The identity of the database table's row referencing this module.</param>
        public UserControlAsset(int moduleId)
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetModuleByID", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ModuleID", moduleId);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            this.moduleID = moduleId;
                            this.moduleType = rdr["ModuleType"].ToString() == "True" ? UserControlArea.Right : UserControlArea.Main;
                            this.moduleName = (string)rdr["ModuleName"];
                            this.modulePath = (string)rdr["ModulePath"];
                            this.description = (string)rdr["ModuleDescription"];
                        }
                        else
                        {
                            throw new DataException("Module does not exist.");
                        }
                    }
                }
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the module's database row identity.
        /// </summary>
        public int ModuleId
        {
            get
            {
                return this.moduleID;
            }
        }

        /// <summary>
        /// Gets or sets the module's given name.
        /// </summary>
        public string ModuleName
        {
            get
            {
                return this.moduleName;
            }

            set
            {
                this.moduleName = value;
            }
        }

        /// <summary>
        /// Gets or sets the module's path.
        /// </summary>
        public string ModulePath
        {
            get
            {
                return this.modulePath;
            }

            set
            {
                this.modulePath = value;
            }
        }

        /// <summary>
        /// Gets or sets the module's type.
        /// </summary>
        public UserControlArea ModuleType
        {
            get
            {
                return this.moduleType;
            }

            set
            {
                this.moduleType = value;
            }
        }

        /// <summary>
        /// Gets or sets the module's description.
        /// </summary>
        public string Description
        {
            get
            {
                return this.description;
            }

            set
            {
                this.description = value;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets a dictionary of modules for the current application (determined by the connection's sql user account.)
        /// </summary>
        /// <param name="moduleType">The type of modules to return (Main or Right).</param>
        /// <returns>A dictionary object containing row identity keys and the referenced module's given name.</returns>
        public static IList<UserControlAsset> GetModules(UserControlArea moduleType)
        {
            List<UserControlAsset> m = new List<UserControlAsset>();
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetModules", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ModuleType", moduleType == UserControlArea.Right ? 1 : 0);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                m.Add(new UserControlAsset(Convert.ToInt32(rdr["ModuleID"], CultureInfo.InvariantCulture)));
                            }
                        }
                    }
                }
            }

            return m.AsReadOnly();
        }

        /// <summary>
        /// Removes all references to this UserControl, as well as the UserControl registration. It does not delete the ascx file.
        /// </summary>
        public void Delete()
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteModule", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ModuleID", this.moduleID);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// Inserts the HTemplate object into the database.
        /// </summary>
        public void SaveAs()
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertModule", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ModuleType", this.moduleType == UserControlArea.Right ? 1 : 0);
                    cmd.Parameters.AddWithValue("@ModuleName", this.moduleName);
                    cmd.Parameters.AddWithValue("@ModulePath", this.modulePath);
                    cmd.Parameters.AddWithValue("@ModuleDescription", this.description);
                    this.moduleID = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }
        }
        #endregion
    }
}